package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.FunctionItemBean;

/**
 * 常用功能适配器
 */
public final class CommonFunctionAdapter extends AppAdapter<FunctionItemBean> {

    private OnRemoveClickListener mRemoveListener;
    private boolean isEditMode = true; // 默认为编辑模式

    public CommonFunctionAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CommonHolder();
    }

    public void setOnRemoveClickListener(OnRemoveClickListener listener) {
        this.mRemoveListener = listener;
    }
    
    /**
     * 设置是否为编辑模式
     * @param isEditMode true-编辑模式（显示减号），false-查看模式（隐藏减号）
     */
    public void setEditMode(boolean isEditMode) {
        this.isEditMode = isEditMode;
    }

    private final class CommonHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView ivIcon;
        private final TextView tvName;
        private final ImageView ivRemove;

        private CommonHolder() {
            super(R.layout.function_item_normal);
            ivIcon = findViewById(R.id.iv_function_icon);
            tvName = findViewById(R.id.tv_function_name);
            ivRemove = findViewById(R.id.iv_add);
        }

        @Override
        public void onBindView(int position) {
            FunctionItemBean item = getItem(position);
            ivIcon.setImageResource(item.getIconRes());
            tvName.setText(item.getName());
            ivRemove.setImageResource(R.drawable.ic_label_reduce);

            // 根据模式显示/隐藏减号
            ivRemove.setVisibility(isEditMode ? View.VISIBLE : View.GONE);

            ivRemove.setOnClickListener(v -> {
                if (mRemoveListener != null) {
                    mRemoveListener.onRemoveClick(position, item);
                }
            });
        }
    }

    public interface OnRemoveClickListener {
        void onRemoveClick(int position, FunctionItemBean item);
    }
}

